//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AssetSwapHelper.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CAssetSwapHelper::CAssetSwapHelper (boost::shared_ptr<QuantLib::AssetSwapHelper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAssetSwapHelper = NULL;
#endif
	_ppAssetSwapHelper = &childNative;
    
}
Cephei::QL::Experimental::Credit::CAssetSwapHelper::CAssetSwapHelper (QuantLib::AssetSwapHelper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAssetSwapHelper = NULL;
#endif
	_ppAssetSwapHelper = new boost::shared_ptr<QuantLib::AssetSwapHelper> (&childNative);
    
    _AssetSwapHelperOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CAssetSwapHelper::CAssetSwapHelper (CAssetSwapHelper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAssetSwapHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAssetSwapHelper = new boost::shared_ptr<QuantLib::AssetSwapHelper> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CAssetSwapHelper::CAssetSwapHelper (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAssetSwapHelper = NULL;
#endif
	if (!t->IsSubclassOf(CAssetSwapHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CAssetSwapHelper::CAssetSwapHelper (QuantLib::Handle<QuantLib::AssetSwapHelper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAssetSwapHelper = &childNative;
	_ppAssetSwapHelper = &static_cast<boost::shared_ptr<QuantLib::AssetSwapHelper>>(childNative.currentLink());
    
    _AssetSwapHelperOwner = owner;
}
Cephei::QL::Experimental::Credit::CAssetSwapHelper::CAssetSwapHelper (QuantLib::Handle<QuantLib::AssetSwapHelper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAssetSwapHelper = &childNative;
	_ppAssetSwapHelper = &static_cast<boost::shared_ptr<QuantLib::AssetSwapHelper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CAssetSwapHelper::CAssetSwapHelper (QuantLib::AssetSwapHelper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAssetSwapHelper = NULL;
#endif
	_ppAssetSwapHelper = new boost::shared_ptr<QuantLib::AssetSwapHelper> (new QuantLib::AssetSwapHelper (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CAssetSwapHelper::~CAssetSwapHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAssetSwapHelper != NULL)
    {
	    delete _ppAssetSwapHelper;
        _ppAssetSwapHelper = NULL;
    }
}
Cephei::QL::Experimental::Credit::CAssetSwapHelper::!CAssetSwapHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAssetSwapHelper != NULL)
    {
	    delete _ppAssetSwapHelper;
    }
}
QuantLib::AssetSwapHelper& Cephei::QL::Experimental::Credit::CAssetSwapHelper::GetReference ()
{
    if (_ppAssetSwapHelper == NULL) throw gcnew NativeNullException ();
	return **_ppAssetSwapHelper;
}
boost::shared_ptr<QuantLib::AssetSwapHelper>& Cephei::QL::Experimental::Credit::CAssetSwapHelper::GetShared ()
{
    if (_ppAssetSwapHelper == NULL) throw gcnew NativeNullException ();
	return *_ppAssetSwapHelper;
}
QuantLib::AssetSwapHelper* Cephei::QL::Experimental::Credit::CAssetSwapHelper::GetPointer ()
{
    if (_ppAssetSwapHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppAssetSwapHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AssetSwapHelper>& Cephei::QL::Experimental::Credit::CAssetSwapHelper::GetHandle ()
{
	if (_phAssetSwapHelper == NULL)
	{
		_phAssetSwapHelper = new Handle<QuantLib::AssetSwapHelper> (*_ppAssetSwapHelper);
	}
	return *_phAssetSwapHelper;
}
#endif
bool Cephei::QL::Experimental::Credit::CAssetSwapHelper::HasNative () 
{
	return (_ppAssetSwapHelper != NULL);
}

Double Cephei::QL::Experimental::Credit::CAssetSwapHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAssetSwapHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IAssetSwapHelper^ Cephei::QL::Experimental::Credit::CAssetSwapHelper::SetTermStructure (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ ts)
{
    CDefaultProbabilityTermStructure^ _Cts;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cts = safe_cast<CDefaultProbabilityTermStructure^> (ts);
        _Cts->Lock();
        QuantLib::DefaultProbabilityTermStructure* _ts = static_cast<QuantLib::DefaultProbabilityTermStructure*> (_Cts->GetPointer ()); 
    	(*_ppAssetSwapHelper)->setTermStructure ( _ts );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cts != nullptr) _Cts->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

